package com.nightsoul.shirotest.session.factory;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.SessionFactory;
import org.apache.shiro.web.session.mgt.WebSessionContext;

import com.nightsoul.shirotest.session.OnlineSession;
import com.nightsoul.shirotest.web.util.IpUtils;

public class OnlineSessionFactory implements SessionFactory {

	@Override
	public Session createSession(SessionContext initData) {
		OnlineSession onlineSession = new OnlineSession();
		if(initData instanceof WebSessionContext) {
			WebSessionContext webSessionContext = (WebSessionContext) initData;
			HttpServletRequest request = (HttpServletRequest) webSessionContext.getServletRequest();
			if(request != null) {
				onlineSession.setHost(IpUtils.getIpAddr(request));
				onlineSession.setUserAgent(request.getHeader("User-Agent"));
				onlineSession.setSystemHost(request.getLocalAddr() + ":" + request.getLocalPort());
			}
		}

		return onlineSession;
	}

}
